<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px ">
    <el-form-item label="电影名称" prop="filmName">
      <el-input v-model="dataForm.filmName" placeholder="请输入..."></el-input>
    </el-form-item>
    <el-form-item label="上传电影资源" prop="filmUrl">
      <upload v-model="dataForm.filmUrl"></upload>
    </el-form-item>
    <el-form-item label="排序" prop="sorted">
      <el-input v-model="dataForm.sorted" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="热点值" prop="hotPoint">
      <el-input v-model="dataForm.hotPoint" placeholder="热点值"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import upload from "@/components/upload/Upload"
  export default {
    components: {upload},
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          resourceSn: '',
          filmName: '',
          filmStore: '',
          filmUrl: 'https://renrenFilm.oss-cn-hangzhou.aliyuncs.com/2022-11-20/5f3b826f-9b28-46a7-8bfe-f6c188ae2e73_871743713-1-208.mp4',
          createdAt: '',
          createdBy: '',
          modifiedAt: '',
          modifiedBy: '',
          sorted: '',
          hotPoint: '',
          playCount: '',
          isDeleted: ''
        },
        dataRule: {
          filmName: [
            { required: true, message: '电影名称不能为空', trigger: 'blur' }
          ],
          filmUrl: [
            { required: true, message: '资源存储地址不能为空', trigger: 'blur' }
          ],
          sorted: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          hotPoint: [
            { required: true, message: '热点值不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/film/film/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.resourceSn = data.film.resourceSn
                this.dataForm.filmName = data.film.filmName
                this.dataForm.filmStore = data.film.filmStore
                this.dataForm.filmUrl = data.film.filmUrl
                this.dataForm.createdAt = data.film.createdAt
                this.dataForm.createdBy = data.film.createdBy
                this.dataForm.modifiedAt = data.film.modifiedAt
                this.dataForm.modifiedBy = data.film.modifiedBy
                this.dataForm.sorted = data.film.sorted
                this.dataForm.hotPoint = data.film.hotPoint
                this.dataForm.playCount = data.film.playCount
                this.dataForm.isDeleted = data.film.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/film/film/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'resourceSn': this.dataForm.resourceSn,
                'filmName': this.dataForm.filmName,
                'filmStore': this.dataForm.filmStore,
                'filmUrl': this.dataForm.filmUrl,
                'createdAt': this.dataForm.createdAt,
                'createdBy': this.dataForm.createdBy,
                'modifiedAt': this.dataForm.modifiedAt,
                'modifiedBy': this.dataForm.modifiedBy,
                'sorted': this.dataForm.sorted,
                'hotPoint': this.dataForm.hotPoint,
                'playCount': this.dataForm.playCount,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
